package luz.javaproxy.plugins.ContentReplacer;


import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.LinkedList;
import java.util.List;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;

import luz.javaproxy.common.Tuple;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class ContentReplacerTable extends JTable{
	private static final long serialVersionUID = 534648762781480107L;
	private final MyTableModel model;
	private static final Log log = LogFactory.getLog(ContentReplacerTable.class);
	
	public ContentReplacerTable(List<Tuple<String, File>> replacerList){
		super();
		model=new MyTableModel(replacerList);
		this.setModel(model);
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		this.getColumnModel().getColumn(1).setCellEditor(new MyTableCellEditor());

	}

	public void addLine() {
		model.addLine();
	}
	
	public void removeSelectedLine() {
		int row = this.getSelectedRow();
		if (row!=-1)
			model.removeLine(row);
		
	}
	
	private static class MyTableModel extends AbstractTableModel {
		private static final long serialVersionUID = 7780019195084742274L;
		private String[] columnNames = {"Address", "File"};
	    private List<Tuple<String, File>> replacerList = new LinkedList<Tuple<String, File>>();
	    
	    public MyTableModel(List<Tuple<String, File>> replacerList){
	    	this.replacerList=replacerList;
	    }
	    	
		public void addLine(){
			replacerList.add(new Tuple<String, File>("", null));
			int last=getRowCount();
			fireTableRowsInserted(last, last);
		}
	    
		public void removeLine(int row) {
			replacerList.remove(row);
			fireTableRowsDeleted(row, row);
		}
	    
	    @Override
	    public int getColumnCount() {
	        return columnNames.length;
	    }
	    
	    @Override
	    public int getRowCount() {
	        return replacerList.size();
	    }
	    
	    @Override
	    public String getColumnName(int col) {
	        return columnNames[col];
	    }
	    
	    @Override
	    public Object getValueAt(int row, int col) {
	    	switch (col){
	    		case 0:
	    			return replacerList.get(row).getFirst();
	    		case 1:
	    			return replacerList.get(row).getSecond();
	    		default:
	    			return null;
	    	}
	    }
	    
		@Override
	    @SuppressWarnings("unchecked")
	    public Class getColumnClass(int c) {
	    	Object r = getValueAt(0, c);
	        return r!=null?r.getClass():Object.class;
	    }

	    @Override
	    public boolean isCellEditable(int row, int col) {
	    	return true;
        }
	    
	    @Override
        public void setValueAt(Object value, int row, int col) {
	    	switch (col){
	    		case 0:
		    		replacerList.get(row).setFirst((String)value);
		    		break;
	    		case 1: 
		    		replacerList.get(row).setSecond((File)value);
		    		break;
	    	}
            fireTableCellUpdated(row, col);
        }
	}



	public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor, MouseListener  {
		private static final long serialVersionUID = -845135716129309399L;
		JFileChooser fileChooser;
		JLabel triggerLabel;
		JButton triggerButton;
		Object currentFile;

		public MyTableCellEditor(){
			triggerLabel=new JLabel();
			triggerLabel.setOpaque(true);
			triggerLabel.setForeground(UIManager.getColor("Table.selectionForeground"));
			triggerLabel.setBackground(UIManager.getColor("Table.selectionBackground"));
			triggerLabel.addMouseListener(this);
	        fileChooser = new JFileChooser();
		}
		
		@Override
		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) {
			currentFile=value;									//save old value
			triggerLabel.setText(value==null?"":value.toString());
			return triggerLabel;
		}
		
		@Override
		public Object getCellEditorValue() {
			return currentFile;									//Return old or new value
		}
		
		@Override
		public void mouseClicked(MouseEvent e) {
			if (e.getClickCount()>1){
				int response = fileChooser.showDialog(triggerButton, null);
				if (response==JFileChooser.APPROVE_OPTION)
					currentFile = fileChooser.getSelectedFile();	//set new value
			}
			fireEditingStopped();
		}
		
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
	}


	
}
